package wec_wallet.ebo.com.activity.register;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.doublewave.DoubleWaveView;

import org.json.JSONObject;

import bassproject.ebo.com.ebobass.activity.MyBaseActivity;
import bassproject.ebo.com.ebobass.http_utils.interfaces.IHttpPostListener;
import bassproject.ebo.com.ebobass.http_utils.utils.Ipost;
import bassproject.ebo.com.ebobass.http_utils.utils.LoadingUtils;
import bassproject.ebo.com.ebobass.http_utils.utils.NetState;
import bassproject.ebo.com.ebobass.utils.IToast;
import bassproject.ebo.com.ebobass.utils.I_Share;
import bassproject.ebo.com.ebobass.utils.Lg;
import bassproject.ebo.com.ebobass.utils.StringUtil;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wec_wallet.ebo.com.R;
import wec_wallet.ebo.com.utils.Functions;
import wec_wallet.ebo.com.utils.PubRequest;

public class LoginActivity extends MyBaseActivity {
//
    @BindView(R.id.actionbar_back)
    RelativeLayout actionbar_back;
    @BindView(R.id.et_username)
    EditText et_username;
    @BindView(R.id.btn_ok)
    Button btn_ok;
    @BindView(R.id.waveView)
    DoubleWaveView waveView;
    @BindView(R.id.iv_rotate)
    ImageView iv_rotate;
    @BindView(R.id.btn_sign)
    TextView btn_sign;
    @BindView(R.id.et_password)
    EditText et_password;
    boolean drawback = false;
    Animation animation;
    LinearInterpolator lin;
    String phone, code, ip;
    Handler handlerLogin = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 1:
                    PubRequest.login(LoginActivity.this, ip, phone, code, new ListenerLogin(code), false);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initViews();
    }

    private void init_anim() {
        //旋转动画
        animation = AnimationUtils.loadAnimation(this, R.anim.img_animation);
        lin = new LinearInterpolator();
        //设置动画匀速运动
        animation.setInterpolator(lin);
    }

    private void rotate_anim(boolean run) {
        if (run) {
            iv_rotate.startAnimation(animation);
        } else {
            iv_rotate.clearAnimation();
        }

    }

    @Override
    public void initViews() {
        btn_sign.setVisibility(View.GONE);
        init_anim();
        rotate_anim(true);
//        waveView.setAnim(false);
        String username = I_Share.readAccountUsername_Phone();
        String password = I_Share.readAccountPasswordLogin();
        if (!StringUtil.isEmpty(username)) {
            et_username.setText(username);

        }
        if (!StringUtil.isEmpty(password)) {
            et_password.setText(password);
        }
//        Intent intent = getIntent();
//        drawback = intent.getBooleanExtra("drawback", false);
//        actionbar_back.setVisibility(View.VISIBLE);

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onClick_Back();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
    }

    //返回键
    @OnClick(R.id.actionbar_back)
    void onClick_Back() {
        exit();
    }

    //登录按钮
    @OnClick(R.id.btn_ok)
    void clickOK() {
        phone = et_username.getText().toString();
        code = et_password.getText().toString();
        //账号是否为空
        if (phone == null || TextUtils.isEmpty(phone)) {
            IToast.show(getResources().getString(R.string.login_hint_input_username));
            return;
        }
        //登录密码是否为空
        if (code == null || TextUtils.isEmpty(code)) {
            IToast.show(getResources().getString(R.string.login_hint_input_password));
            return;
        }
        btn_ok.setClickable(false);
        btn_sign.setClickable(false);
//        rotate_anim(true);
//        LoadingUtils.progressDialogShow(this);
        new Thread(new Runnable() {
            @Override
            public void run() {
                ip = NetState.getNetIp();
                LoadingUtils.progressDialogDismiss();
                handlerLogin.sendEmptyMessage(1);
            }
        }).start();
    }

    //注册
    @OnClick(R.id.btn_sign)
    void clickSign() {
        Intent intent = new Intent();
        intent.setClass(this, SignActivity.class);
        startActivity(intent);
        finish();
    }

    //接口返回 start======================================================================================================================

    //登录
    class ListenerLogin implements IHttpPostListener {
        String password;

        public ListenerLogin(String password) {
            this.password = password;
        }

        @Override
        public void onTimeOut() {
            btn_ok.setClickable(true);
            btn_sign.setClickable(true);
//            rotate_anim(false);
        }

        @Override
        public void responseSuccess(String result) {
            btn_ok.setClickable(true);
            btn_sign.setClickable(true);
            rotate_anim(false);
            if (Ipost.isTimeOut(result)) {
                return;
            }
            Lg.d("登录 result=" + result);
            if (null != result) {
                try {
                    JSONObject resultObject = new JSONObject(result);
                    int error = resultObject.getInt("error");
                    String message = "";
                    if (resultObject.has("error_msg") && error != 0) {
                        message = resultObject.getString("error_msg");
                        IToast.show(message);
                    }
                    if (error == 0) {
                        //成功
//                        show_Toast("登录成功");
                        String user_id = resultObject.getString("id");
                        String mobile = resultObject.getString("mobile");
                        String nickname = resultObject.getString("nickname");
                        String avatar_path = resultObject.getString("avatar_path");//头像地址
                        String avatar = resultObject.getString("avatar");//头像文件夹
                        String token = resultObject.getString("remember_token");
                        String name = resultObject.getString("name");
                        int prefix = resultObject.getInt("prefix");
                        String is_remote = resultObject.getString("is_remote");
                        I_Share.saveAccount(mobile, null, password, token, user_id);
                        I_Share.setMobilePrefix(prefix);
                        I_Share.setNickname(nickname);
                        I_Share.setAvatar(avatar_path + avatar);
                        if (is_remote.equals("1")) {
                            //ip异常
                            I_Share.setIsLogin(false);
                            Intent intent = new Intent();
                            intent.setClass(LoginActivity.this, ErrorLoginActivity.class);
                            startActivity(intent);
                            finish();
                        } else {

                            I_Share.setIsLogin(true);
                            Functions.startMainActivity(LoginActivity.this);
                        }

                    }
                } catch (Exception e) {
                    // TODO: handle exception
                    Lg.d(getClass() + ",接口解析异常：\n" + e.toString());
                }
            }
        }
    }


    //接口返回 end======================================================================================================================


}
